package com.leha.mp.core.services.imp;
import com.cloopen.rest.sdk.CCPRestSDK;
import com.leha.mp.core.services.HidingCallServices;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Set;

@Service
public class HidingCallServicesImpl implements HidingCallServices {
    private String serverIP = ResourceBundle.getBundle("config").getString("serverIP");
    private String port = ResourceBundle.getBundle("config").getString("port");
    private String account = ResourceBundle.getBundle("config").getString("account");
    private String token = ResourceBundle.getBundle("config").getString("token");
    private String subaccount = ResourceBundle.getBundle("config").getString("subaccount");
    private String subsid = ResourceBundle.getBundle("config").getString("subsid");
    private String appid = ResourceBundle.getBundle("config").getString("appid");
    private String tone = ResourceBundle.getBundle("config").getString("tone");
    private String countDownTone = ResourceBundle.getBundle("config").getString("countDownTone");
    private String landingCallTone = ResourceBundle.getBundle("config").getString("landingCallTone");

    @Override
    public HashMap<String, Object> MakeDoubleSideCall(String from, String to, Integer limitsInSec, String callBackUrl, String Data) {
        HashMap<String, Object> result = null;
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(serverIP, port);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount(account, token);;// 初始化主帐号和主帐号TOKEN
        restAPI.setSubAccount(subaccount,subsid);
        restAPI.setAppId(appid);// 初始化应用ID010
        // 设置回呼号码
        result = restAPI.callback(from, to,"","",tone,"true",
                "",Data,limitsInSec.toString(),callBackUrl,"","1","120",countDownTone);
        System.out.println("SDKTestSetnumber result=" + result);
        if("000000".equals(result.get("statusCode"))){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            HashMap<String, Object> ret = (HashMap<String, Object>) data.get("CallBack");
            return ret;
        }else{
            return result;
        }
    }

    @Override
    public Boolean HanupCall(String callSid) {
        HashMap<String, Object> result = null;
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(serverIP, port);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount(account, token);;// 初始化主帐号和主帐号TOKEN
        restAPI.setSubAccount(subaccount,subsid);
        restAPI.setAppId(appid);// 初始化应用ID010
        // 设置回呼号码
        result = restAPI.CallCancel(callSid, "0");
        System.out.println("SDKTestSetnumber result=" + result);
        if("000000".equals(result.get("statusCode"))){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean MakeLandingCalls(String to) {
        HashMap<String, Object> result = null;
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(serverIP, port);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount(account, token);;// 初始化主帐号和主帐号TOKEN
        restAPI.setSubAccount(subaccount,subsid);
        restAPI.setAppId(appid);// 初始化应用ID010
        // 设置回呼号码
       // result = restAPI.landingCall(to, "before.wav", "", "",
                //"2", 0,"http://test.pule.com","","","","","","");
        result = restAPI.landingCall(to, landingCallTone, "", "",
                "2", 0,"http://test.pule.com","","","","","","");
        System.out.println("SDKTestSetnumber result=" + result);
        if("000000".equals(result.get("statusCode"))){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            HashMap<String, Object> ret = (HashMap<String, Object>) data.get("CallBack");
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean MakeLandingCallsWithCustomContent(String to, String content) {
        HashMap<String, Object> result = null;
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(serverIP, port);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
        restAPI.setAccount(account, token);;// 初始化主帐号和主帐号TOKEN
        restAPI.setSubAccount(subaccount,subsid);
        restAPI.setAppId(appid);// 初始化应用ID010
        // 设置回呼号码
        // result = restAPI.landingCall(to, "before.wav", "", "",
        //"2", 0,"http://test.pule.com","","","","","","");
        result = restAPI.landingCall(to, "", content, "",
                "2", 0,"http://test.pule.com","","","","","","");
        System.out.println("SDKTestSetnumber result=" + result);
        if("000000".equals(result.get("statusCode"))){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            HashMap<String, Object> ret = (HashMap<String, Object>) data.get("CallBack");
            return true;
        }else{
            return false;
        }
    }
}
